package com.qiantu.api.db;

import android.content.Context;
import android.text.TextUtils;

import com.qiantu.api.db.database.LocalDataBase;
import com.qiantu.api.db.greendao.DeviceBeanDao;
import com.qiantu.api.db.greendao.UniversalDeviceDataDao;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.UniversalDeviceData;

import java.util.List;

public class LLDeviceActionDBManager {
    private final String TAG = "LLDeviceActionDBManager";
    private volatile static LLDeviceActionDBManager instance = null;
    private final static Object object = new Object();
    private final UniversalDeviceDataDao mUniversalDeviceDataDao;
    private Context context;

    private LLDeviceActionDBManager(Context context) {
        this.context = context;
        mUniversalDeviceDataDao = LocalDataBase.getInstance().getDaoSession(context).getUniversalDeviceDataDao();
    }

    public static LLDeviceActionDBManager getInstance(Context context) {
        if (instance == null) {
            synchronized (object) {
                if (instance == null) {
                    instance = new LLDeviceActionDBManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void clearFromDB() {
        mUniversalDeviceDataDao.deleteAll();
    }

    /**
     * 从服务器更新设备数据
     *
     * @param universalDeviceData
     */
    public void refreshAllSoucecesTx(List<UniversalDeviceData> universalDeviceData, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            clearFromDB();
            if(universalDeviceData == null){
                return;
            }
            for (UniversalDeviceData data : universalDeviceData) {
                mUniversalDeviceDataDao.save(data);
            }
        });
    }
    /**
     * 保存或者更新群组设备
     *
     * @param universalDeviceData
     */
    public void addBatch(List<UniversalDeviceData> universalDeviceData) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (UniversalDeviceData data : universalDeviceData) {
                mUniversalDeviceDataDao.save(data);
            }
        });
    }

    public void put(UniversalDeviceData bean) {
        if (bean == null)
            return;
        String serialNo = bean.getSerialNo();
        if (serialNo != null && !TextUtils.isEmpty(serialNo)) {
            UniversalDeviceData oldData = mUniversalDeviceDataDao.queryBuilder()
                    .where(UniversalDeviceDataDao.Properties.SerialNo.eq(serialNo))
                    .unique();
            if (oldData != null) {
                bean.setLocalId(oldData.getLocalId());
            }
        }
        mUniversalDeviceDataDao.save(bean);
    }

    public List<UniversalDeviceData> getDeviceDatasByDeviceSerialNo(String deviceSerialNo) {
        return mUniversalDeviceDataDao.queryBuilder()
                .where(UniversalDeviceDataDao.Properties.DeviceSerialNo.eq(deviceSerialNo))
                .list();
    }

    public List<UniversalDeviceData> getEnalbeDeviceDatasByDeviceSerialNo(String deviceSerialNo) {
        return mUniversalDeviceDataDao.queryBuilder()
                .where(UniversalDeviceDataDao.Properties.DeviceSerialNo.eq(deviceSerialNo),
                        UniversalDeviceDataDao.Properties.Enable.eq(true))
                .orderAsc(UniversalDeviceDataDao.Properties.Sort)
                .list();
    }

    public List<UniversalDeviceData> getDeviceKeyDatasByDeviceSerialNo(String deviceSerialNo,String key) {
        return mUniversalDeviceDataDao.queryBuilder()
                .where(UniversalDeviceDataDao.Properties.DeviceSerialNo.eq(deviceSerialNo),
                        UniversalDeviceDataDao.Properties.ActionIdentifier.eq(key),
                        UniversalDeviceDataDao.Properties.Enable.eq(true))
                .orderAsc(UniversalDeviceDataDao.Properties.Sort)
                .list();
    }
}
